﻿using SWD = System.Web.Services.Description;

namespace MockingBird.Wsdl.Parser
{

    public class Operation
    {
        public string Name { get; private set; }
        public OperationMessage InputMessage { get; private set; }
        public OperationMessage OutputMessage { get; private set; }

        /// <summary>
        /// Constructs a new WsdlOperation instance from a standard Operation instance
        /// </summary>
        /// <param name="op"></param>
        public Operation(SWD.ServiceDescription desc, SWD.Operation op)
        {
            this.Name = op.Name;
            InputMessage = new OperationMessage(desc, op.Messages.Input);
            //build the operation only if this is a two way message
            if ((op.Messages.Flow == System.Web.Services.Description.OperationFlow.RequestResponse)
                || (op.Messages.Flow == System.Web.Services.Description.OperationFlow.SolicitResponse))
            {
                OutputMessage = new OperationMessage(desc, op.Messages.Output);
            }
        }

        public OperationMessage FindMessage(string messageName)
        {
            //When dealing with output messages we have to cater to one way operations
            if (this.InputMessage.Name.Equals(messageName))
                return this.InputMessage;
            else
                if ((this.OutputMessage != null) && (this.OutputMessage.Name.Equals(messageName)))
                    return this.OutputMessage;
                else
                    return null;

        }


    }
}
